package st.spider;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class HttpTookit {
	public static String doGet(String url, List<NameValuePair> params,
			CharEncoding charset) {
		String responseContent = "";
		CloseableHttpClient httpclient = HttpClients.createDefault();
		try {
			// 创建httpget.
			HttpGet httpget = new HttpGet(url);
			CloseableHttpResponse response = httpclient.execute(httpget);
			try {
				// 获取响应实体
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					responseContent = EntityUtils.toString(entity,
							charset.value());
					// 打印响应内容
					// System.out
					// .println("--------------------------------------");
					// System.out.println("Response content: " +
					// responseContent);
					// System.out.println("------------------------------------");
				}
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return responseContent;
	}

	public static String doPost(String url, List<NameValuePair> params,
			CharEncoding charset) {
		// 创建默认的httpClient实例.
		String responseContent = "";
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost httppost = new HttpPost(url);
		UrlEncodedFormEntity uefEntity;
		try {
			uefEntity = new UrlEncodedFormEntity(params, charset.value());
			httppost.setEntity(uefEntity);
			CloseableHttpResponse response = httpclient.execute(httppost);
			try {
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					responseContent = EntityUtils.toString(entity,
							charset.value());
					// System.out
					// .println("--------------------------------------");
					// System.out.println("Response content: " +
					// responseContent);
					// System.out
					// .println("--------------------------------------");
				}
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return responseContent;
	}

	public static String doPost(String url, String body, CharEncoding charset) {
		// 创建默认的httpClient实例.
		String responseContent = "";
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost httppost = new HttpPost(url);
		try {
			StringEntity uefEntity = new StringEntity(body, charset.value());
			httppost.setEntity(uefEntity);
			CloseableHttpResponse response = httpclient.execute(httppost);
			try {
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					responseContent = EntityUtils.toString(entity,
							charset.value());
					// System.out
					// .println("--------------------------------------");
					// System.out.println("Response content: " +
					// responseContent);
					// System.out
					// .println("--------------------------------------");
				}
			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return responseContent;
	}

	public static void main(String[] args) {
		String html = doGet("http://www.xuandy.com/movie/54220.html", null,
				CharEncoding.UTF8);
		// System.out.println(html);
		Document doc = Jsoup.parse(html);
		Elements els = doc.getElementsByAttributeValue("class", "entry");

		String temp = els.get(0).toString();
		temp = temp
				.replace(
						"<a href=\"http://www.xuandy.com/\" title=\"炫小编\" target=\"_blank\">炫小编</a>",
						"<a href=\"user/admin\" title=\"小编\" target=\"_blank\">小编</a>");
		temp = temp
				.replace("http://www.xuandy.com/tag", "movie/tag")
				.replace(
						"下载地址：<a href=\"http://www.xuandy.com/\">http://www.xuandy.com/</a> <br />",
						"");
		System.out.println(temp);
		
		System.out.println("《热浴盆时光机2》高清迅雷下载".replace("《", "").replace("》高清迅雷下载", ""));
	}
}
